﻿Imports System.Data.SqlClient
Imports C1.Win.C1List.C1Combo
Imports C1.Win.C1FlexGrid
Imports System.Windows.Forms.DataGrid
Imports DevComponents.DotNetBar.Controls.DataGridViewX
Public Class Cls_DanhMuc
    Dim str As String
    Public strError As String = ""


    Sub LayDLLoaiSP(ByVal dtg As DataGrid, ByVal dts As DataSet)
        Try
            Dim sqlADT As New SqlDataAdapter("select * from LOAISP", con)
            dts = New DataSet()
            sqlADT.Fill(dts, "LOAISP")
            dtg.DataSource = dts.Tables("LOAISP")
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu từ cơ sở dự liệu Loại SP", MsgBoxStyle.Critical, "Thông báo")

        End Try
    End Sub
    Sub LayDLCTPhieuNhap(ByVal dtg As DataGrid, ByVal dts As DataSet, ByVal txt As TextBox)
        Try
            Dim sqlADT As New SqlDataAdapter("select * from CTPhieuNhap a,SanPham b where a.masp=b.masp and mapn='" & txt.Text & "'", con)
            dts = New DataSet()
            sqlADT.Fill(dts, "CTPhieuNhap")
            dtg.DataSource = dts.Tables("CTPhieuNhap")
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu từ cơ sở dự liệu CT Phiếu Nhập", MsgBoxStyle.Critical, "Thông báo")

        End Try
    End Sub
    Sub FillCombo(ByVal cbx As ComboBox, ByVal strsql As String, ByVal display As String, ByVal value As String)
        Try
            With cbx
                .DataSource = Getdata(strsql)
                .DisplayMember = display
                .ValueMember = value

            End With
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu combobox!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Sub FillC1ComBo(ByVal cbx As C1.Win.C1List.C1Combo, ByVal strsql As String, ByVal display As String, ByVal value As String)
        Try
            With cbx
                .DataSource = Getdata(strsql)
                .DisplayMember = display
                .ValueMember = value

            End With
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu combobox!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Sub FillDTG(ByVal strsql As String, ByVal table As String, ByVal dtg As DataGrid, ByVal dts As DataSet)
        Try
            Dim sqlADT As New SqlDataAdapter(strsql, con)
            dts = New DataSet()
            sqlADT.Fill(dts, table)
            dtg.DataSource = dts.Tables(table)
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu lên lưới!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Sub FillDataGird(ByVal strsql As String, ByVal dtg As DataGrid, ByVal dts As DataSet)
        Try
            dtg.DataSource = Getdata(strsql)
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu lên lưới!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Sub FillDataGirdView(ByVal strsql As String, ByVal dtg As DataGridView, ByVal dts As DataSet)
        Try
            dtg.DataSource = Getdata(strsql)
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu lên lưới GirdView!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Sub FillDataGirdC1(ByVal strsql As String, ByVal dtg As C1FlexGrid, ByVal dts As DataSet)
        Try
            dtg.DataSource = Getdata(strsql)
        Catch ex As Exception
            MsgBox("Lỗi lấy dữ liệu lên lưới GirdView!!!", MsgBoxStyle.Critical, "Thông Báo")
        End Try
    End Sub
    Public Sub FillDataText(ByRef txt As TextBox, ByVal proc As String)
        Dim myData As New SqlCommand(proc, con)
        Try
            Dim sqladt As SqlDataReader
            sqladt = myData.ExecuteReader
            sqladt.Read()
            txt.Text = sqladt(0)
            sqladt.Close()
        Catch ex As Exception
            MsgBox("Lỗi lấy DL Text:" & Chr(13) & ex.Message, , "Thông Báo")
        End Try
    End Sub
    Function Getdata(ByVal strsql As String) As DataTable
        Dim mydata As New DataTable
        Dim sqladt As New SqlDataAdapter(strsql, con)
        Try
            sqladt.Fill(mydata)
        Catch ex As Exception
            MsgBox("Lỗi lấy thông tin từ CSDL", MsgBoxStyle.Critical, "Thông Báo")
        End Try
        Return mydata
    End Function
    Public Function GetValue( _
    ByRef myDT As DataTable, _
    ByVal strSQL As String) As String
        Dim adData As SqlDataAdapter
        Try
            adData = New SqlDataAdapter(strSQL, con)
            adData.Fill(myDT)
            strError = "OK"
        Catch ex As Exception
            strError = ex.Message
        Finally

        End Try
        Return strError
    End Function
    Public Function OpenConnection() As Boolean
        Try
            con.Open()
            Return True
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function
    Public Sub CloseConnection()
        con.Close()
    End Sub
    Public Function ExecuteCommandReader(ByVal SqlCmd As String) As SqlDataReader

        Dim m_Cmd = New SqlCommand(SqlCmd, con)
        Return m_Cmd.ExecuteReader()
    End Function
    Public Function DangNhap(ByVal strUserName As String, ByVal strPassword As String) As Boolean
        OpenConnection()

        Dim bSuccess As Boolean = ExecuteCommandReader("Select * From TaiKhoan Where TenTK = '" & _
                strUserName & "' AND Matkhau = '" & strPassword + "'").Read()
        CloseConnection()
        Return bSuccess

    End Function



End Class
